package com.weibo.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.weibo.entity.WeiboToken;
import com.weibo.service.WeiboService;

public class WeiboFabuServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
		doPost(request,response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
		WeiboService ws = new WeiboService();
		String isFa = request.getParameter("fa");
		String weibotext = request.getParameter("weibotext");
		String zhanghaoids[] = request.getParameterValues("zhanghaoids");
		
		List<WeiboToken> wts = ws.findAllToken();
		
		if(isFa == null){//第一次访问直接访问页面
			request.setAttribute("wts", wts);
			request.getRequestDispatcher("/weibo_fabu.jsp").forward(request, response);
		}else if("fa".endsWith(isFa)){//有关注请求的时候
			request.setAttribute("wts", wts);
			if(zhanghaoids != null){
				Long ids[] =new Long[zhanghaoids.length];
				for(int i = 0; i < zhanghaoids.length; i++){
					ids[i]= Long.parseLong(zhanghaoids[i]);
				}
				List< WeiboToken> ts = ws.findTokenByids(ids);
				ws.piliangfa(ts,weibotext,null);
			}
			request.setAttribute("success", "任务执行成功！！！");
			request.getRequestDispatcher("/weibo_fabu.jsp").forward(request, response);
		}
	
		
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
